package com.example.xinyun.slice;

import cn.droidlover.xrecyclerview.XRecyclerView;
import com.example.xinyun.ResourceTable;
import com.example.xinyun.domain.UrlContant;
import com.example.xinyun.db.TbFourm;
import com.example.xinyun.db.TbFourmList;
import com.example.xinyun.provider.TbFourmProvider;
import com.google.gson.Gson;
import com.zzrv5.mylibrary.ZZRCallBack;
import com.zzrv5.mylibrary.ZZRHttp;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.window.dialog.ToastDialog;

import java.util.ArrayList;

public class TzDatilsAbilitySlice extends AbilitySlice {
    private ArrayList<TbFourm> data;
    private TbFourmProvider provide;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_tz_datils);

        findComponentById(ResourceTable.Id_to).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent intent = new Intent();
                Operation operation = new Intent.OperationBuilder().withDeviceId("")
                        .withBundleName("com.xijing.xinyun")
                        .withAbilityName("com.example.xinyun.FPinglunAbility")
                        .build();
                intent.setParam("id",getAbility().getIntent().getIntParam("id",0));
                intent.setOperation(operation);
                startAbility(intent,0);
            }
        });
        data = new ArrayList<>();
        XRecyclerView listContainer = (XRecyclerView) findComponentById(ResourceTable.Id_xr);
        listContainer.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                data.clear();
                initData();
            }

            @Override
            public void onLoadMore() {

            }
        });
        provide = new TbFourmProvider(this, data);
        if(getAbility().getIntent().getStringParam("isown").equals("1")){
            provide.setIsFTR(true);
        }else{
            provide.setIsFTR(false);
        }
        listContainer.setItemProvider(provide);
        initData();
    }

    private void initData() {
        String url = UrlContant.URL+"selectFourmlist?tzid="+getAbility().getIntent().getIntParam("id",0);
        ZZRHttp.get(url, new ZZRCallBack.CallBackString() {

            @Override
            //http访问出错，此部分在主线程中工作,可以更新UI等操做。
            public void onFailure(int code, String errorMessage) {
                new ToastDialog(getAbility())
                        .setText("加载失败，请检查网络连接！")
                        .show();
            }

            @Override
            //http访问成功，此部分在主线程中工作，可以更新UI等操作。
            public void onResponse(String response) {
                //将http请求返回的json字符串转换成一个dbPsychological类的对象
                TbFourmList object = new Gson().fromJson(response, TbFourmList.class);

                if (object.getData() != null) {
                    if (data.size() != 0) {
                        data.clear();
                    }
                    data.addAll(object.getData());
                    provide.notifyDataChanged();
                }
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
